//
//  EJSOrderDetailViewController.m
//  EnjoyShoppingStreet
//
//  Created by lifayu on 2018/1/3.
//  Copyright © 2018年 lifayu. All rights reserved.
//

#import "EJSOrderDetailViewController.h"

#import "EJSOrderGoodsTableViewCell.h"
#import "EJSOrderDetailStatusTableViewCell.h"
#import "EJSOrderDetailPayInfoTableViewCell.h"
#import "EJSOrderDetailOrderInfoTableViewCell.h"
#import "EJSSettleSelectAddressCell.h"
#import "EJSOrderDetailGoodsTitleTableViewCell.h"

#import "EJSOrderDetailRequest.h"

@interface EJSOrderDetailViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *orderOpertaionViewHeight;
@property (weak, nonatomic) IBOutlet UIButton *opertaionButton;
@property (weak, nonatomic) IBOutlet UIButton *cancelButton;


@end

@implementation EJSOrderDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"订单详情";
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self getOrderData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)configOwnViews{
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.01)];
    self.tableView.tableFooterView = [UIView new];
    self.tableView.estimatedRowHeight = 100;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"EJSOrderGoodsTableViewCell" bundle:nil] forCellReuseIdentifier:@"EJSOrderGoodsTableViewCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"EJSOrderDetailStatusTableViewCell" bundle:nil] forCellReuseIdentifier:@"EJSOrderDetailStatusTableViewCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"EJSOrderDetailPayInfoTableViewCell" bundle:nil] forCellReuseIdentifier:@"EJSOrderDetailPayInfoTableViewCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"EJSOrderDetailOrderInfoTableViewCell" bundle:nil] forCellReuseIdentifier:@"EJSOrderDetailOrderInfoTableViewCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"EJSSettleSelectAddressCell" bundle:nil] forCellReuseIdentifier:@"EJSSettleSelectAddressCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"EJSOrderDetailGoodsTitleTableViewCell" bundle:nil] forCellReuseIdentifier:@"EJSOrderDetailGoodsTitleTableViewCell"];
}

- (void)getOrderData{
    EJSOrderDetailRequest *req = [EJSOrderDetailRequest new];
    req.orderId = self.orderModel.orderId;
    FlyWeakSelf
    [req asyncRequestWithsuccessHandler:^(BaseResponse *response) {
        weakSelf.orderModel = response.data;
    } failHandler:^(BaseResponse *response) {
        [[HUDHelper sharedInstance] tipMessage:response.message];
    }];
}

- (void)setOrderModel:(EJSOrderModel *)orderModel{
    if(_orderModel != orderModel){
        _orderModel = orderModel;
        
        self.cancelButton.hidden = YES;
        self.orderOpertaionViewHeight.constant = 0;
        if(_orderModel.flag.integerValue == 1){
            self.cancelButton.hidden = NO;
            self.orderOpertaionViewHeight.constant = 175;
            [self.opertaionButton setTitle:@"立即付款" forState:UIControlStateNormal];
        }else if(_orderModel.flag.integerValue == 3){
            self.orderOpertaionViewHeight.constant = 100;
            [self.opertaionButton setTitle:@"确认收货" forState:UIControlStateNormal];
        }else if(_orderModel.flag.integerValue == 4){
            self.orderOpertaionViewHeight.constant = 0;
            [self.opertaionButton setTitle:@"评价" forState:UIControlStateNormal];
            [self.opertaionButton setHidden:YES];
        }
        [self.view layoutIfNeeded];
        [self.tableView reloadData];
    }
}

- (IBAction)orderOperationAction:(UIButton *)sender {
    NSString *operation = [sender titleForState:UIControlStateNormal];
    if([operation isEqualToString:@"立即付款"]){
        [self payFoyOrder:_orderModel];
    }else if ([operation isEqualToString:@"查看物流"]){
        [self checkLogisticsOrder:_orderModel];
    }else if ([operation isEqualToString:@"取消订单"]){
        [self cancelOrder:_orderModel];
    }else if ([operation isEqualToString:@"确认收货"]){
        [self receivedOrder:_orderModel];
    }else if ([operation isEqualToString:@"评价"]){
        [self commentOrder:_orderModel];
    }
}

- (IBAction)cancelAction:(id)sender {
    [self cancelOrder:_orderModel];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.orderModel.goods.count + 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row == 0){
        EJSOrderDetailStatusTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EJSOrderDetailStatusTableViewCell" forIndexPath:indexPath];
        [cell configCellWithOrderStatus:self.orderModel.flag orderStatusText:self.orderModel.flagText expressCorpName:self.orderModel.expressCorpName expressNo:self.orderModel.expressNo];
        return cell;
    }else if (indexPath.row == 1){
        EJSSettleSelectAddressCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EJSSettleSelectAddressCell" forIndexPath:indexPath];
        cell.selectAddressView.hidden = YES;
        cell.nameLabel.text = [NSString stringWithFormat:@"%@   %@ %@",[EJSUserModel countryCode],self.orderModel.receiveMan,self.orderModel.receivePhone];
        cell.addressLabel.text = self.orderModel.receiveAddress;
        
        return cell;
    }else if (indexPath.row == 2){
        EJSOrderDetailGoodsTitleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EJSOrderDetailGoodsTitleTableViewCell" forIndexPath:indexPath];
        return cell;
        
    }else if (indexPath.row > 2 && indexPath.row < self.orderModel.goods.count + 3){
        EJSOrderGoodsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EJSOrderGoodsTableViewCell" forIndexPath:indexPath];
        [cell setGoodsModel:self.orderModel.goods[indexPath.row - 3]];
        return cell;
    }else if(indexPath.row == self.orderModel.goods.count + 3){
        EJSOrderDetailPayInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EJSOrderDetailPayInfoTableViewCell" forIndexPath:indexPath];
        [cell configCell:self.orderModel.totalAmount freight:self.orderModel.expressFee rebate:self.orderModel.totalRebate];
        return cell;
    }else{
        EJSOrderDetailOrderInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EJSOrderDetailOrderInfoTableViewCell" forIndexPath:indexPath];
        [cell configCell:self.orderModel.sn payMethod:self.orderModel.paymethodText payNo:self.orderModel.transactionId orderTime:self.orderModel.createTime];
        return cell;
    }
    return nil;
}

@end
